package com.me.mygdxgame;


import java.util.Iterator;

import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.audio.Music;
import com.badlogic.gdx.audio.Sound;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.utils.Array;
import com.badlogic.gdx.utils.TimeUtils;

public class GLTest implements ApplicationListener {
	Texture eggImage;
	Texture bucketImage;
	Texture chickenImage;
	Texture duckImage;
	Texture geeseImage;
	Texture shitImage;
	Sound dropSound;
	Music rainMusic;
	BitmapFont font;
	SpriteBatch batch;
	OrthographicCamera camera;
	Rectangle bucket;
	Array<Rectangle> eggdrops;
	Array<Rectangle> shitdrops;
	Array<Rectangle> chickens;
	Array<Long> chickenEggTime;
	Array<Rectangle> ducks;
	Array<Long> duckEggTime;
	Array<Rectangle> geeses;
	Array<Long> geeseEggTime;
	long lastEggTime;
	long lastPressTime;
	int score;
	int life = 3;
	@Override
	public void create() {
		// load the images for the droplet and the bucket, 64x64 pixels each
	    eggImage = new Texture(Gdx.files.internal("egg.png"));
	    bucketImage = new Texture(Gdx.files.internal("Ro.png"));
	    chickenImage = new Texture(Gdx.files.internal("ga.png"));
	    duckImage = new Texture(Gdx.files.internal("vit.png"));
	    geeseImage = new Texture(Gdx.files.internal("ngan.png"));
	    shitImage = new Texture(Gdx.files.internal("shit.png"));
	      
	    camera = new OrthographicCamera();
	    camera.setToOrtho(false, 800, 480);
	    batch = new SpriteBatch();
	    font = new BitmapFont(Gdx.files.internal("data/font.fnt"), Gdx.files.internal("data/font.png") ,false);
	      // create a Rectangle to logically represent the bucket
	    bucket = new Rectangle();
	    bucket.x = 800 / 2 - 64 / 2; // center the bucket horizontally
	    bucket.y = 20; // bottom left corner of the bucket is 20 pixels above the bottom screen edge
	    bucket.width = 64;
	    bucket.height = 64;
	      
	      // create the eggdrops array and spawn the first eggdrop
	    eggdrops = new Array<Rectangle>();
	    chickens = new Array<Rectangle>();
	    ducks = new Array<Rectangle>();
	    geeses = new Array<Rectangle>();
	    shitdrops = new Array<Rectangle>();
	    chickenEggTime = new Array<Long>();
	    duckEggTime = new Array<Long>();
	    geeseEggTime = new Array<Long>();
	}
	
	@Override
	public void render() {
	    Gdx.gl.glClearColor(0, 0, 0.2f, 1);
	    Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
	   
	    camera.update();
	    //TODO Batch : draw all 
	    batch.setProjectionMatrix(camera.combined);
	    batch.begin();
	    
	    batch.draw(bucketImage, bucket.x, bucket.y);
	    font.setColor(1.0f, 1.0f, 1.0f, 1.0f);
	    font.draw(batch, "" + score, 120, 470);
	    
	    font.setColor(1.0f, 1.0f, 1.0f, 1.0f);
	    font.draw(batch, ""+ life, 25, 470);
	    
	    
	    for(Rectangle chicken: chickens) {
	    	batch.draw(chickenImage, chicken.x, chicken.y);
	    }
	    for(Rectangle duck: ducks) {
	    	batch.draw(duckImage, duck.x, duck.y);
	    }
	    for(Rectangle geese: geeses) {
	    	batch.draw(geeseImage, geese.x, geese.y);
	    }
	    for (Rectangle egg : eggdrops) {
				batch.draw(eggImage, egg.x, egg.y);
		}
	    for (Rectangle shit : shitdrops) {
			batch.draw(shitImage, shit.x, shit.y);
	    }
	    batch.end();
	      
	    InputKey();
	      
	    AllMove(chickens);
		AllMove(ducks);
	    AllMove(geeses);
	    
	    Iterator<Rectangle> iter = eggdrops.iterator();
	    while(iter.hasNext()) {
	    	Rectangle egg = iter.next();
	    	if(score / 400 > 0)
	    		egg.y -= 200 *(1 + (score / 400 * 0.2)) * Gdx.graphics.getDeltaTime();
	    	else
	    		egg.y -= 200 *  Gdx.graphics.getDeltaTime();
		    if(egg.y + 64 < 0)
		    {
		    	iter.remove();
		    	life -= 1;
		    	if(life == 0)
		    	{
		    		pause();
		    	}
		    }
		    if(egg.overlaps(bucket)) {
		    	score += 20;
		    	iter.remove();
		    }
	    }
	    Iterator<Rectangle> iter2 = shitdrops.iterator();
	    while(iter2.hasNext()) {
	         Rectangle egg = iter2.next();
	         if(score / 400 > 0)
		    		egg.y -= 200 *(1 + (score / 400 * 0.2)) *  Gdx.graphics.getDeltaTime();
	         else
		    		egg.y -= 200 *  Gdx.graphics.getDeltaTime();
	         if(egg.y + 64 < 0) 
	         {
	        	 iter2.remove();
	         }
	         if(egg.overlaps(bucket)) {
	            iter2.remove();
	            life -= 1;
	            if(life == 0)
		    	{
		    		pause();
		    	}
	         }
	    }
	}
	
	private void makerectanglebird(int type) {
		Rectangle bird = new Rectangle();
		bird.x = MathUtils.random(0, 800-64);
	    bird.y = 380;
	    bird.width = 64;
	    bird.height = 64;
	    if(type == 0)
		{
			chickens.add(bird);
			chickenEggTime.add(TimeUtils.millis());
		}
		else if(type == 1) {
			ducks.add(bird);
			duckEggTime.add(TimeUtils.millis());
		}
		else {
			geeses.add(bird);
			geeseEggTime.add(TimeUtils.millis());
		}
	    lastPressTime = TimeUtils.nanoTime();
	}
	//TODO Make egg
	private void makeEgg(Rectangle rec)
	{
		Rectangle egg = new Rectangle();
		egg.x = rec.x;
	    egg.y = rec.y;
	    egg.width = 64;
	    egg.height = 64;
	    if(MathUtils.random(0, 1) == 0)
	    	eggdrops.add(egg);
	    else
	    	shitdrops.add(egg);
	}
	//TODO AllMove
	public void AllMove(Array<Rectangle> arr)
	{
		Iterator<Rectangle> iter = arr.iterator();
		Iterator<Long> iterTime;
		if (arr == chickens) {
			iterTime = chickenEggTime.iterator();
		}
		else if(arr == ducks)
		{
			iterTime = duckEggTime.iterator();
		}
		else
		{
			iterTime = geeseEggTime.iterator();
		}
	    while(iter.hasNext()) {
	    	final Rectangle rec = iter.next();
	    	Long time = iterTime.next();
	    	Dichuyen(rec);
	    	if((TimeUtils.millis() - time) % 5000  < 16)
	    		makeEgg(rec);
	    }
	}
	//TODO DIchuye
	public void Dichuyen(Rectangle rec)
	{
		int flag = 0;
		if(rec.x +64 < 800 && flag == 0)
    		rec.x += 200*Gdx.graphics.getDeltaTime();
    	else
    	{
    		rec.x =0;
    	}
	}
	//TODO Input key
	public void InputKey()
	{
		if(Gdx.input.isTouched()) {
	    	Vector3 touchPos = new Vector3();
	    	touchPos.set(Gdx.input.getX(), Gdx.input.getY(), 0);
	    	camera.unproject(touchPos);
	    	bucket.x = touchPos.x - 64 / 2;
	    }
	    if(Gdx.input.isKeyPressed(Keys.G))
	    {
	    	if (TimeUtils.nanoTime() - lastPressTime > 200000000 ) {
	    		makerectanglebird(0);
			}
	    	
	    }
	    if(Gdx.input.isKeyPressed(Keys.V))
	    {
	    	if (TimeUtils.nanoTime() - lastPressTime > 200000000 ) {
	    		makerectanglebird(1);
			}
	    }
	    if(Gdx.input.isKeyPressed(Keys.N))
	    {
	    	if (TimeUtils.nanoTime() - lastPressTime > 200000000 ) {
	    		makerectanglebird(2);
			}
	    }
	      
	    if(Gdx.input.isKeyPressed(Keys.LEFT)) bucket.x -= 200 * Gdx.graphics.getDeltaTime();
	    if(Gdx.input.isKeyPressed(Keys.RIGHT)) bucket.x += 200 * Gdx.graphics.getDeltaTime();
	}
	
	@Override
	public void dispose() {
		// dispose of all the native resources
		
	    eggImage.dispose();
	    bucketImage.dispose();
	    dropSound.dispose();
	    rainMusic.dispose();
	    batch.dispose();
	}

	@Override
	public void resize(int width, int height) {
	}

	@Override
	public void pause() {
	}

	@Override
	public void resume() {
	}
}
